{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 下载板块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "xtdata.download_sector_data()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取所有板块列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.get_sector_list()\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取板块成分股"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.get_stock_list_in_sector('沪深A股')\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 按交易所代码获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.get_stock_list_in_sector('BJ') #SZ，BJ\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 指数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.get_stock_list_in_sector('沪深指数')\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "xtdata.download_index_weight()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.get_index_weight('000300.SH')\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 股票"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "res = xtdata.get_stock_list_in_sector('沪深A股')\n",
    "\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "def sh_zbb():\n",
    "    \"\"\"\n",
    "    获取上交所股票列表，并过滤出上证主板股票（60XXXX.SH）\n",
    "    \"\"\"\n",
    "    sh_stock_list = xtdata.get_stock_list_in_sector('SH')  # 获取上交所股票列表\n",
    "    return [stock for stock in sh_stock_list if stock.startswith('60') and stock.endswith('.SH')]\n",
    "\n",
    "def sh_kcb():\n",
    "    \"\"\"\n",
    "    获取上交所股票列表，并过滤出上证科创板股票（68XXXX.SH）\n",
    "    \"\"\"\n",
    "    sh_stock_list = xtdata.get_stock_list_in_sector('SH')  # 获取上交所股票列表\n",
    "    return [stock for stock in sh_stock_list if stock.startswith('68') and stock.endswith('.SH')]\n",
    "\n",
    "def sz_zbb():\n",
    "    \"\"\"\n",
    "    获取深交所股票列表，并过滤出深证主板股票（00XXXX.SZ）\n",
    "    \"\"\"\n",
    "    sz_stock_list = xtdata.get_stock_list_in_sector('SZ')  # 获取深交所股票列表\n",
    "    return [stock for stock in sz_stock_list if stock.startswith('00') and stock.endswith('.SZ')]\n",
    "\n",
    "def sz_cyb():\n",
    "    \"\"\"\n",
    "    获取深交所股票列表，并过滤出深证创业板股票（30XXXX.SZ）\n",
    "    \"\"\"\n",
    "    sz_stock_list = xtdata.get_stock_list_in_sector('SZ')  # 获取深交所股票列表\n",
    "    return [stock for stock in sz_stock_list if stock.startswith('30') and stock.endswith('.SZ')]\n",
    "\n",
    "def bj():\n",
    "    \"\"\"\n",
    "    获取北交所股票列表，并过滤出北交所股票（83XXXX.BJ 或 43XXXX.BJ）\n",
    "    \"\"\"\n",
    "    bj_stock_list = xtdata.get_stock_list_in_sector('BJ')  # 获取北交所股票列表\n",
    "    return [stock for stock in bj_stock_list if (stock.startswith('83') or stock.startswith('43')) and stock.endswith('.BJ')]\n",
    "\n",
    "# 示例使用\n",
    "if __name__ == \"__main__\":\n",
    "    # 调用函数获取过滤后的股票列表\n",
    "    sh_zbb_list = sh_zbb()  # 上证主板\n",
    "    sh_kcb_list = sh_kcb()  # 上证科创板\n",
    "    sz_zbb_list = sz_zbb()  # 深证主板\n",
    "    sz_cyb_list = sz_cyb()  # 深证创业板\n",
    "    bj_list = bj()  # 北交所\n",
    "\n",
    "    # 输出结果\n",
    "    print(\"上证主板:\", sh_zbb_list)\n",
    "    print(\"上证科创板:\", sh_kcb_list)\n",
    "    print(\"深证主板:\", sz_zbb_list)\n",
    "    print(\"深证创业板:\", sz_cyb_list)\n",
    "    print(\"北交所股票:\", bj_list)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基金"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "res = xtdata.get_stock_list_in_sector('沪市基金')\n",
    "\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "res = xtdata.get_stock_list_in_sector('深市基金')\n",
    "\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hs_jj():\n",
    "    \"\"\"\n",
    "    获取沪市基金列表（5XXXXX.SH）\n",
    "    \"\"\"\n",
    "    sh_stock_list = xtdata.get_stock_list_in_sector('SH')  # 获取上交所股票列表\n",
    "    return [stock for stock in sh_stock_list if stock.startswith('5') and stock.endswith('.SH')]\n",
    "\n",
    "def sz_jj():\n",
    "    \"\"\"\n",
    "    获取深市基金列表（15XXXX.SH, 16XXXX.SH, 18XXXX.SH）\n",
    "    \"\"\"\n",
    "    sz_stock_list = xtdata.get_stock_list_in_sector('SZ')  # 获取深交所股票列表\n",
    "    return [stock for stock in sz_stock_list if (stock.startswith('15') or stock.startswith('16') or stock.startswith('18')) and stock.endswith('.SZ')]\n",
    "\n",
    "# 示例使用\n",
    "if __name__ == \"__main__\":\n",
    "    # 调用函数获取过滤后的基金列表\n",
    "    hs_jj_list = hs_jj()  # 沪市基金\n",
    "    sz_jj_list = sz_jj()  # 深市基金\n",
    "\n",
    "    # 输出结果\n",
    "    print(\"沪市基金:\", hs_jj_list)\n",
    "    print(\"深市基金:\", sz_jj_list)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 可转债"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "res = xtdata.get_stock_list_in_sector('上证转债')\n",
    "\n",
    "res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "res = xtdata.get_stock_list_in_sector('深证转债')\n",
    "\n",
    "res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sh_zz():\n",
    "    \"\"\"\n",
    "    获取上证转债列表（11XXXX.SH）\n",
    "    \"\"\"\n",
    "    sh_stock_list = xtdata.get_stock_list_in_sector('SH')  # 获取上交所股票列表\n",
    "    return [stock for stock in sh_stock_list if stock.startswith('11') and stock.endswith('.SH')]\n",
    "\n",
    "def sz_zz():\n",
    "    \"\"\"\n",
    "    获取深证转债列表（12XXXX.SZ）\n",
    "    \"\"\"\n",
    "    sz_stock_list = xtdata.get_stock_list_in_sector('SZ')  # 获取深交所股票列表\n",
    "    return [stock for stock in sz_stock_list if stock.startswith('12') and stock.endswith('.SZ')]\n",
    "\n",
    "# 示例使用\n",
    "if __name__ == \"__main__\":\n",
    "    # 调用函数获取过滤后的转债列表\n",
    "    sh_zz_list = sh_zz()  # 上证转债\n",
    "    sz_zz_list = sz_zz()  # 深证转债\n",
    "\n",
    "    # 输出结果\n",
    "    print(\"上证转债:\", sh_zz_list)\n",
    "    print(\"深证转债:\", sz_zz_list)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 逆回购"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sh_gznhg():\n",
    "    \"\"\"\n",
    "    获取上证国债逆回购列表（204XXX.SH）\n",
    "    \"\"\"\n",
    "    sh_stock_list = xtdata.get_stock_list_in_sector('SH')  # 获取上交所股票列表\n",
    "    return [stock for stock in sh_stock_list if stock.startswith('204') and stock.endswith('.SH')]\n",
    "\n",
    "def sz_gznhg():\n",
    "    \"\"\"\n",
    "    获取深证国债逆回购列表（131XXX.SZ）\n",
    "    \"\"\"\n",
    "    sz_stock_list = xtdata.get_stock_list_in_sector('SZ')  # 获取深交所股票列表\n",
    "    return [stock for stock in sz_stock_list if stock.startswith('131') and stock.endswith('.SZ')]\n",
    "\n",
    "# 示例使用\n",
    "if __name__ == \"__main__\":\n",
    "    # 调用函数获取过滤后的国债逆回购列表\n",
    "    sh_gznhg_list = sh_gznhg()  # 上证国债逆回购\n",
    "    sz_gznhg_list = sz_gznhg()  # 深证国债逆回购\n",
    "\n",
    "    # 输出结果\n",
    "    print(\"上证国债逆回购:\", sh_gznhg_list)\n",
    "    print(\"深证国债逆回购:\", sz_gznhg_list)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取标的详情"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "res = xtdata.get_instrument_detail('127030.SZ')\n",
    "res"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
